﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Text;
using System.Data;
using App.Data;
using App.Core.DB;
using App.Core.Extensions;

public partial class _Default : BasePage 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String support = ConfigReader.SupportEmail;
        BindPageInfo();
        if (!IsPostBack)
        {
            BindDropdownLists();
            BindCounts();
            BindFeaturedProjects();
            BindZoneWiseProjectCounts();
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle("Home");
    }
    protected void BindDropdownLists()
    {
        //BindDropdownList.PropertyTypes(ddlType, false);
        //ddlType.Items.Add(new ListItem("On-going Flat", String.Format("{0}-1", ConfigReader.ApartmentPropertyTypeID, "1")));
        //ddlType.Items.Add(new ListItem("Up-coming Flat", String.Format("{0}-4", ConfigReader.ApartmentPropertyTypeID, "1")));
        //ddlType.SetSelectedItem(ConfigReader.ApartmentPropertyTypeID.ToString());
        //ApartmentSizes();
        //LandSizes();
        ////BindDropdownList.Cities(ddlLocation, ConfigReader.SiteBusinessCountryID);
        //BindDropdownList.Cities(ddlLocation, ConfigReader.SiteBusinessCountryID);
        //ddlLocation.SetSelectedItem(ConfigReader.SiteBusinessCityID.ToString());
        ////BindDropdownList.RentTypes(ddlRentType);
        //BindDropdownList.Cities(ddlRentCountry, ConfigReader.SiteBusinessCountryID);
        //ddlRentCountry.SetSelectedItem(ConfigReader.SiteBusinessCityID.ToString());
        //BindDropdownList.Zones(ddlRentZone, ConfigReader.SiteBusinessCityID);
        //BindDropdownList.SubscriptionTypes(ddlSubscriptionTypes);
    }
    protected void BindCounts()
    {
        UtilityDAO dao = new UtilityDAO();
        int apartmentCount = NullHandler.GetInt32(dao.GetScalar(String.Format(AppSQL.GET_PROJECT_COUNT, ConfigReader.ApartmentPropertyTypeID), null, false));
        int landCount = NullHandler.GetInt32(dao.GetScalar(String.Format(AppSQL.GET_PROJECT_COUNT, ConfigReader.LandPropertyTypeID), null, false));
        //ltrApartmentCount.Text = apartmentCount.ToString();
        //ltrLandProjectCount.Text = landCount.ToString();

        int buildingMaterialsCount = dao.GetActiveCountOfServiceProviders(ConfigReader.BuildingMaterialSPID);
        int realEstateServiceProviderCount = dao.GetActiveCountOfServiceProviders(ConfigReader.RealEstateServicesSPID);
        int furnitureServiceProviderCount = dao.GetActiveCountOfServiceProviders(ConfigReader.FurnitureSPID);
        int homeAppliancesCount = dao.GetActiveCountOfServiceProviders(ConfigReader.HomeAppliancesSPID);
        int memberCount = NullHandler.GetInt(dao.GetScalar(AppSQL.GET_ACTIVER_CUSTOMER_COUNT, null, false));

        //ltrBuildingMaterialCompaniesCount.Text = buildingMaterialsCount >0 ? buildingMaterialsCount.ToString() : String.Empty;
        //ltrRealEstateServicesCount.Text = realEstateServiceProviderCount > 0 ? realEstateServiceProviderCount.ToString() : String.Empty;
        //ltrFurnitureServiceProviderCount.Text = furnitureServiceProviderCount > 0 ? furnitureServiceProviderCount.ToString() : String.Empty;
        //ltrHomeAppliancesCount.Text = homeAppliancesCount > 0 ? homeAppliancesCount.ToString(): String.Empty;
        //ltrPortalMembersCount.Text = memberCount > 0 ? memberCount.ToString(): String.Empty;
    }
    protected void BindFeaturedProjects()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var projectsQuery = from P in context.Projects.Include("Customer")                       
                       where P.IsHot && !P.IsDeleted && P.Customer.IsApproved && !P.Customer.IsDeleted                       
                       select P;
        
        StringBuilder leftHTML = new StringBuilder(10);
        StringBuilder rightHTML = new StringBuilder(10);
        if (projectsQuery != null)
        {
            List<Project> projects = projectsQuery.Take(8).ToList();
            for (int i = 0; i < projects.Count; i++)
            {
                if (i % 2 == 0)
                    BindFeaturedProjects(context, projects[i], leftHTML);
                else
                    BindFeaturedProjects(context, projects[i], rightHTML);
            }
            ltrFeaturedProjectsLeft.Text = leftHTML.ToString();
            ltrFeaturedProjectsRight.Text = rightHTML.ToString();
        }
    }

    protected void BindFeaturedProjects(BtcHousingEntities context, Project project, StringBuilder sectionHtml)
    {
        String imageHtml = "<img class='FeaturedHomepageProjectThumb' src='{0}/{1}/{2}' alt='Featured' title='Featured' />";
        String html = @"<div class='ProjectBox'>
                            <div class='ProjectThumb'>{0}</div>
                            <div class='ProjectLink'>
                                <a href='{1}?ID={2}'>{3}</a><br />
                                {4}
                            </div>
                            <div class='clearboth'></div>
                        </div>";
        var projectFile = project.ProjectFiles.FirstOrDefault(P => P.FileTypeID != ConfigReader.ProjectBrocheureFileTypeID);
        String thumImage = projectFile == null ? String.Empty :
                String.Format(imageHtml, AppConstants.PROJECT_FILES, project.ID, projectFile.ThumbFileName);
        
        sectionHtml.AppendFormat(html, thumImage, AppConstants.Pages.PROJECT_DETAILS, project.ID, project.Name.HtmlEncode(), project.Customer.Name.HtmlEncode());                
    }   
    /// <summary>
    /// Binds the Search Apartment By Zone/Area List
    /// </summary>
    protected void BindZoneWiseProjectCounts()
    {
        UtilityDAO dao = new UtilityDAO();
        App.Data.DbParameter[] parameters = new[] {
            new App.Data.DbParameter("@CountryID", ConfigReader.SiteBusinessCountryID),
            new App.Data.DbParameter("@PropertyTypeID", ConfigReader.ApartmentPropertyTypeID)
        };
        DataSet ds = dao.GetDataSet(AppSQL.GET_ZONE_WISE_PROJECT_COUNT_BY_COUNTRY, parameters, false);
        if (!ds.IsEmpty())
        {
//            String tableHtml = @"<table cellpadding='3' cellspacing='0' style='width:100%;'>
//                                    {0}
//                                 </table>";
            DataTable dt = ds.Tables[0];
            String prevCity = String.Empty;
            int noOfCells = 4;
            int addedCells = 0;
            StringBuilder sb = new StringBuilder(@"<table cellpadding='3' cellspacing='0' style='width:100%;'>
                            <colgroup>
                                <col style='width:25%;'/>
                                <col style='width:25%;'/>
                                <col style='width:25%;'/>
                                <col />
                            </colgroup>");
            //StringBuilder zoneTable = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                String city = NullHandler.GetString(row["City"]);
                if (String.Compare(city, prevCity, true) != 0)
                {
                    string tbodyID = String.Format("City_{0}", NullHandler.GetInt(row["CityID"]));
                    if (i == 0)
                    {
                        sb.AppendFormat("<tr><td colspan='{0}'><div class='CityHeading CityHeadingFirst'><a href=\"javascript:void(0);\" onclick=\"ShowHideZones('{2}');\">{1}</a></div></td></tr>", noOfCells, city, tbodyID);
                        sb.AppendFormat(@"<tbody id='{0}' style='display:none;'>", tbodyID);
                    }
                    else
                    {
                        sb.Append("</tbody>");
                        //sb.AppendFormat("<tr><td colspan='{0}'><div class='CityHeading'>{1}</div></td></tr>", noOfCells, city);
                        sb.AppendFormat("<tr><td colspan='{0}'><div class='CityHeading'><a href=\"javascript:void(0);\" onclick=\"ShowHideZones('{2}');\">{1}</a></div></td></tr>", noOfCells, city, tbodyID);
                        sb.AppendFormat(@"<tbody id='{0}' style='display:none;'>", tbodyID);
                    }
                    
                    
                    addedCells = 0;
                }
                if (addedCells == 0)
                    sb.Append("<tr>");
                sb.AppendFormat("<td><a href='{0}'>{1}({2})</a></td>", GetProjectListLink(NullHandler.GetInt(row["ZoneID"])), NullHandler.GetString(row["Zone"]), NullHandler.GetInt(row["ProjectCount"]));
                addedCells++;                
                prevCity = city;
                String nextCity = i + 1 <= dt.Rows.Count - 1 ? NullHandler.GetString(dt.Rows[i + 1]["City"]) : String.Empty;
                if ((addedCells > 0 && (addedCells % noOfCells) == 0) || nextCity != city)
                {
                    sb.Append("</tr>");
                    addedCells = 0;
                }
            }
            sb.Append("</tbody>");
            sb.Append("</table>");
            ltrZoneWiseProjectCount.Text = sb.ToString();
        }
    }

    private string GetProjectListLink(int zoneID)
    {
        return String.Format("{0}?{1}={2}", AppConstants.Pages.PROJECT_LIST_PUBLIC, AppConstants.QueryString.ZONE_ID, zoneID);
    }
    //public void ApartmentSizes()
    //{
    //    BtcHousingEntities context = new BtcHousingEntities();
    //    var unit = context.Units.FirstOrDefault(P => P.ID != ConfigReader.LandUnitID);
    //    ddlSize.Items.Add(new ListItem(unit == null ? String.Empty : unit.Name, String.Empty)); 
    //    ddlSize.Items.Add(new ListItem("300", "300"));
    //    ddlSize.Items.Add(new ListItem("600", "600"));
    //    ddlSize.Items.Add(new ListItem("800", "800"));
    //    ddlSize.Items.Add(new ListItem("900", "900"));
    //    ddlSize.Items.Add(new ListItem("1000", "1000"));
    //    ddlSize.Items.Add(new ListItem("1200", "1200"));
    //    ddlSize.Items.Add(new ListItem("1500", "1500"));
    //    ddlSize.Items.Add(new ListItem("1800", "1800"));
    //    ddlSize.Items.Add(new ListItem("2200", "2200"));
    //    ddlSize.Items.Add(new ListItem("2700", "2700"));
    //    ddlSize.Items.Add(new ListItem("4500", "4500"));

    //    ddlSizeTo.Items.Add(new ListItem(unit == null ? String.Empty : unit.Name, String.Empty)); 
    //    ddlSizeTo.Items.Add(new ListItem("600", "600"));
    //    ddlSizeTo.Items.Add(new ListItem("800", "800"));
    //    ddlSizeTo.Items.Add(new ListItem("900", "900"));
    //    ddlSizeTo.Items.Add(new ListItem("1000", "1000"));
    //    ddlSizeTo.Items.Add(new ListItem("1200", "1200"));
    //    ddlSizeTo.Items.Add(new ListItem("1500", "1500"));
    //    ddlSizeTo.Items.Add(new ListItem("1800", "1800"));
    //    ddlSizeTo.Items.Add(new ListItem("2200", "2200"));
    //    ddlSizeTo.Items.Add(new ListItem("2700", "2700"));
    //    ddlSizeTo.Items.Add(new ListItem("4500", "4500"));
    //    ddlSizeTo.Items.Add(new ListItem("7000", "7000"));
    //    //throw new NotImplementedException();
    //}
    //public void LandSizes()
    //{
    //    BtcHousingEntities context = new BtcHousingEntities();
    //    var unit = context.Units.FirstOrDefault(P => P.ID == ConfigReader.LandUnitID);
    //    ddlLandSizeFrom.Items.Add(new ListItem(unit == null ? String.Empty : unit.Name, String.Empty));
    //    ddlLandSizeFrom.Items.Add(new ListItem("1", "1"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("2", "2"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("3", "3"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("5", "5"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("10", "10"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("20", "20"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("30", "30"));
    //    ddlLandSizeFrom.Items.Add(new ListItem("40", "40"));
    //    //ddlLandSizeFrom.Items.Add(new ListItem("70", "70"));
    //    //ddlLandSizeFrom.Items.Add(new ListItem("100", "100"));
       

    //    ddlLandSizeTo.Items.Add(new ListItem(unit == null ? String.Empty : unit.Name, String.Empty));
    //    ddlLandSizeTo.Items.Add(new ListItem("1", "1"));
    //    ddlLandSizeTo.Items.Add(new ListItem("2", "2"));
    //    ddlLandSizeTo.Items.Add(new ListItem("3", "3"));
    //    ddlLandSizeTo.Items.Add(new ListItem("5", "5"));
    //    ddlLandSizeTo.Items.Add(new ListItem("10", "10"));
    //    ddlLandSizeTo.Items.Add(new ListItem("20", "20"));
    //    ddlLandSizeTo.Items.Add(new ListItem("30", "30"));
    //    ddlLandSizeTo.Items.Add(new ListItem("40", "40"));
    //    ddlLandSizeTo.Items.Add(new ListItem("70", "70"));
    //    ddlLandSizeTo.Items.Add(new ListItem("100", "100"));
    //    //throw new NotImplementedException();
    //}    
}
